package com.zzq.my.jetpacks.jectpackapp.view.decoration;

import android.content.Context;
import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zzq.my.jetpacks.jectpackapp.utils.ABPixelUtil;

/**
 * 描述：class describe
 * 作者：zhuangzeqin
 * 时间: 2023/3/31-9:10
 * 邮箱：zzq@eeepay.cn
 * 备注:
 *         //一行需要显示4个item
 *         int count = 4;
 *         //recyclerView的宽度
 *         int rvWidth = xxxx;
 *         //每个item的宽度
 *         int itemWidth = xxxx;
 *         //计算间距
 *         int space = (rvWidth - count * itemWidth) / (count - 1);
 */
public class GridSpaceDecoration extends RecyclerView.ItemDecoration {
    //间距
    private int mSpace;
    private int mRowSpace;
    private Context context;

    public GridSpaceDecoration(Context context, int dpValue) {
        this.context = context;
        this.mSpace = (int) ABPixelUtil.dp2px(dpValue, context);
        this.mRowSpace = (int) ABPixelUtil.dp2px(18F, context);
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        GridLayoutManager manager = (GridLayoutManager) parent.getLayoutManager();

        //获取每行的个数
        int spanCount = manager.getSpanCount();
        //获取当前位置
        int position = parent.getChildAdapterPosition(view);
        //当前列
        int column = position % spanCount;


        //计算垂直方向上的间距，当不是首行时
        if (position >= spanCount) {
            //添加垂直方向上的间距
//            outRect.top = mSpace;
            outRect.top = mRowSpace;
        }

//        if ((position + 1) % spanCount == 1 )
//        {
//            //todo 每一行的第一个
//            outRect.left =  (int) ABPixelUtil.dp2px(20F, context);
//            outRect.right = (mSpace - (column + 1) * mSpace / spanCount); // spacing - (column + 1) * ((1f /    spanCount) * spacing)
//        }
//        else if ( (position + 1) % spanCount == 0)
//        {
//            outRect.left = (column * mSpace / spanCount); // column * ((1f / spanCount) * spacing)
//            //tood  每一行的最后一个
//            outRect.right =  (int) ABPixelUtil.dp2px(20F, context);
//        }
//        else {
            //计算水平方向的间距
            outRect.left = (column * mSpace / spanCount); // column * ((1f / spanCount) * spacing)
            outRect.right = mSpace - ((column + 1) * mSpace / spanCount); // spacing - (column + 1) * ((1f /    spanCount) * spacing)
//        }
    }
}
